package enembleSerializer;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.LinkedList;
import java.util.List;

import mlp.Mlp;

public class EnsembleSerializer {

	enum ArgNums {
		MLPS_DIR
	}
	
	public static void main(String[] args) throws ClassNotFoundException, IOException {
		
		// read directory with networks
		String mlpsDirectory = args[ArgNums.MLPS_DIR.ordinal()];
		
		// parse networks (i'th network recognizes digit i (0 <= i <= 9)
		List<Mlp> mlps = readMlps(mlpsDirectory);
		
		// serialize list holding the networks
		FileOutputStream writeFile;
		try {
			writeFile = new FileOutputStream("ensemble_mlps.txt");
			ObjectOutputStream fileStream = new ObjectOutputStream(writeFile);
			fileStream.writeObject(mlps);
			fileStream.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	} 
		
	private static List<Mlp> readMlps(String mlpsDirectory) throws IOException, ClassNotFoundException {

		// initialize return value
		List<Mlp> mlpsList = new LinkedList<Mlp>();
		
		// read network for each digit
		for (int digit = 0 ; digit < 10 ; ++digit) {
			
			// read network for current digit
			FileInputStream readFile = new FileInputStream(mlpsDirectory + "/mlp_0_rec_" + digit + ".txt");
			ObjectInputStream fileStream = new ObjectInputStream(readFile);
			
			// add to networks
			mlpsList.add((Mlp)fileStream.readObject());		
			fileStream.close();
		}
		
		// return parsed networks
		return mlpsList;
	}
}
